using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

namespace PocketApps
{
    public class PocketAppsLocalization
    {
        private static CultureInfo _current;
        private static ResourceManager _resMan;

        /// <summary>
        /// The static initialiser.
        /// </summary>
        static PocketAppsLocalization()
        {
            _current = CultureInfo.CurrentUICulture;

            // Get the appropriate resource from the assembly
            Assembly asm = Assembly.GetExecutingAssembly();
            _resMan = new ResourceManager("PocketApps.tblocal", asm);
        }

        /// <summary>
        /// The language name of the current culture in the language of the 
        /// current culture.
        /// </summary>
        public static string NativeName
        {
            get
            {
                return _current.NativeName;
            }
        }

        /// <summary>
        /// The name of the current culture as a 2 letter language code 
        /// (optionally followed by a 2 letter region code)
        /// </summary>
        /// <remarks>For example:
        /// Spanish: es
        /// Spanish in Spain: es-es
        /// Spanish in Mexico: es-mx
        /// </remarks>
        public static string Name
        {
            get
            {
                return _current.Name;
            }
        }

        /// <summary>
        /// Returns an array of cultures for which this application has a
        /// satellite assembly.
        /// </summary>
        public static CultureInfo[] AvailableCultures
        {
            get
            {
                CultureInfo[] cultures = new CultureInfo[2];
                cultures[0] = new CultureInfo("fi-FI");
                cultures[1] = new CultureInfo("en");
                return cultures;
            }
        }

        /// <summary>
        /// Set the culture using the 2 letter ISO name (and optionally region)
        /// </summary>
        /// <param name="cultureName">The 2 letter ISO name (and optionally region)</param>
        /// <remarks>For example: 
        /// For Dutch: <code>LocaleManager.SetCulture("nl");</code>
        /// For Dutch in The Netherlands <code>LocaleManager.SetCulture("nl-nl");</code>
        /// For Dutch in Belgium <code>LocaleManager.SetCulture("nl-be");</code></remarks>
        public static void SetCulture(string cultureName)
        {
            _current = new CultureInfo(cultureName);
        }

        /// <summary>
        /// Get the localised string for the given Id
        /// </summary>
        /// <param name="id">The Id of the string</param>
        /// <returns>The string in the current locale</returns>
        public static string GetString(string id)
        {
            return _resMan.GetString(id, _current);
        }
    }
}
